package com.dmm.vod.controller;


import com.alibaba.excel.EasyExcel;
import com.dmm.result.Result;
import com.dmm.vod.entity.Subject;
import com.dmm.vod.service.SubjectService;
import com.dmm.vod.utils.AccountImportListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author dmm
 * @since 2022-08-26
 */
@RestController
@CrossOrigin
@Api(tags = "课程分类管理")
@RequestMapping("/vod/subject")
public class SubjectController {

    @Autowired
    SubjectService subjectService;

    //查询下一层课程分类
    //根据parent_id
    @ApiOperation("查询下一层的课程分类")
    @GetMapping("getChildSubject/{id}")
    public Result getChildSubject(@PathVariable Long id) {
        List<Subject> list = subjectService.selectList(id);
        return Result.success(list);
    }


    @ApiOperation(value="导出")
    @GetMapping(value = "/exportData")

    public void exportData(HttpServletResponse response) {
        try {
            subjectService.exportData(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ResponseEntity.status(20000);
        response.setStatus(20000);
    }

    @ApiOperation(value = "导入")
    @PostMapping("/importData")
    public Result importData(@RequestPart("file")MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(),Subject.class,new AccountImportListener(subjectService)).sheet().doRead();
        return Result.success(null);
    }







}

